{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DAY6 行列混合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-02</th>\n",
       "      <td>0.865408</td>\n",
       "      <td>-2.301539</td>\n",
       "      <td>1.744812</td>\n",
       "      <td>-0.761207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04</th>\n",
       "      <td>-0.322417</td>\n",
       "      <td>-0.384054</td>\n",
       "      <td>1.133769</td>\n",
       "      <td>-1.099891</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-05</th>\n",
       "      <td>-0.172428</td>\n",
       "      <td>-0.877858</td>\n",
       "      <td>0.042214</td>\n",
       "      <td>0.582815</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-06</th>\n",
       "      <td>-1.100619</td>\n",
       "      <td>1.144724</td>\n",
       "      <td>0.901591</td>\n",
       "      <td>0.502494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-08</th>\n",
       "      <td>-0.267888</td>\n",
       "      <td>0.530355</td>\n",
       "      <td>-0.691661</td>\n",
       "      <td>-0.396754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-09</th>\n",
       "      <td>-0.687173</td>\n",
       "      <td>-0.845206</td>\n",
       "      <td>-0.671246</td>\n",
       "      <td>-0.012665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-10</th>\n",
       "      <td>-1.117310</td>\n",
       "      <td>0.234416</td>\n",
       "      <td>1.659802</td>\n",
       "      <td>0.742044</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-02  0.865408 -2.301539  1.744812 -0.761207\n",
       "2022-01-03  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-01-04 -0.322417 -0.384054  1.133769 -1.099891\n",
       "2022-01-05 -0.172428 -0.877858  0.042214  0.582815\n",
       "2022-01-06 -1.100619  1.144724  0.901591  0.502494\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769\n",
       "2022-01-08 -0.267888  0.530355 -0.691661 -0.396754\n",
       "2022-01-09 -0.687173 -0.845206 -0.671246 -0.012665\n",
       "2022-01-10 -1.117310  0.234416  1.659802  0.742044"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(1)\n",
    "df = pd.DataFrame(np.random.randn(10,4), \n",
    "                  columns=list('ABCD'),\n",
    "                  index = pd.date_range('20220101', periods=10))\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 行列切片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- loc: location 传入的是 label\n",
    "- iloc: integer location 传入的是整数（位置）\n",
    "---\n",
    "\n",
    "1. 中括号\n",
    "2. 行,列\n",
    "3. :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.31903909605709857"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['2022-01-03','A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.319039\n",
       "C    1.462108\n",
       "Name: 2022-01-03 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['2022-01-03',['A','C']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>1.462108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04</th>\n",
       "      <td>-0.322417</td>\n",
       "      <td>1.133769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-05</th>\n",
       "      <td>-0.172428</td>\n",
       "      <td>0.042214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-06</th>\n",
       "      <td>-1.100619</td>\n",
       "      <td>0.901591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.122890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-08</th>\n",
       "      <td>-0.267888</td>\n",
       "      <td>-0.691661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-09</th>\n",
       "      <td>-0.687173</td>\n",
       "      <td>-0.671246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-10</th>\n",
       "      <td>-1.117310</td>\n",
       "      <td>1.659802</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         C\n",
       "2022-01-03  0.319039  1.462108\n",
       "2022-01-04 -0.322417  1.133769\n",
       "2022-01-05 -0.172428  0.042214\n",
       "2022-01-06 -1.100619  0.901591\n",
       "2022-01-07  0.900856 -0.122890\n",
       "2022-01-08 -0.267888 -0.691661\n",
       "2022-01-09 -0.687173 -0.671246\n",
       "2022-01-10 -1.117310  1.659802"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['2022-01-03':,['A','C']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.462107937044974"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[2,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04</th>\n",
       "      <td>-0.322417</td>\n",
       "      <td>-0.384054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-05</th>\n",
       "      <td>-0.172428</td>\n",
       "      <td>-0.877858</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-06</th>\n",
       "      <td>-1.100619</td>\n",
       "      <td>1.144724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-08</th>\n",
       "      <td>-0.267888</td>\n",
       "      <td>0.530355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-09</th>\n",
       "      <td>-0.687173</td>\n",
       "      <td>-0.845206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-10</th>\n",
       "      <td>-1.117310</td>\n",
       "      <td>0.234416</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2022-01-03  0.319039 -0.249370\n",
       "2022-01-04 -0.322417 -0.384054\n",
       "2022-01-05 -0.172428 -0.877858\n",
       "2022-01-06 -1.100619  1.144724\n",
       "2022-01-07  0.900856 -0.683728\n",
       "2022-01-08 -0.267888  0.530355\n",
       "2022-01-09 -0.687173 -0.845206\n",
       "2022-01-10 -1.117310  0.234416"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[2:,:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 行列筛选（进阶）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2022-01-01     True\n",
       "2022-01-02     True\n",
       "2022-01-03     True\n",
       "2022-01-04    False\n",
       "2022-01-05    False\n",
       "2022-01-06    False\n",
       "2022-01-07     True\n",
       "2022-01-08    False\n",
       "2022-01-09    False\n",
       "2022-01-10    False\n",
       "Freq: D, Name: A, dtype: bool"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['A']>0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-02</th>\n",
       "      <td>0.865408</td>\n",
       "      <td>-2.301539</td>\n",
       "      <td>1.744812</td>\n",
       "      <td>-0.761207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-02  0.865408 -2.301539  1.744812 -0.761207\n",
       "2022-01-03  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df['A']>0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[(df['A']>0) & (df['C']<0)]  # and"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04</th>\n",
       "      <td>-0.322417</td>\n",
       "      <td>-0.384054</td>\n",
       "      <td>1.133769</td>\n",
       "      <td>-1.099891</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-03  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-01-04 -0.322417 -0.384054  1.133769 -1.099891"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[(df['A']>1) | (df['D']<-1)]  # or"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 输入的expr是str\n",
    "2. 列名存在空格，需要用 1 左边按键 的那个符号\n",
    "3. 可以引用变量，用@"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-02</th>\n",
       "      <td>0.865408</td>\n",
       "      <td>-2.301539</td>\n",
       "      <td>1.744812</td>\n",
       "      <td>-0.761207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-02  0.865408 -2.301539  1.744812 -0.761207\n",
       "2022-01-03  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.query('A>0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.query('A>0 and (C<0 or D>0)') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04</th>\n",
       "      <td>-0.322417</td>\n",
       "      <td>-0.384054</td>\n",
       "      <td>1.133769</td>\n",
       "      <td>-1.099891</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-03  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-01-04 -0.322417 -0.384054  1.133769 -1.099891"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.query('A>1 or D<-1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-02</th>\n",
       "      <td>0.865408</td>\n",
       "      <td>-2.301539</td>\n",
       "      <td>1.744812</td>\n",
       "      <td>-0.761207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04</th>\n",
       "      <td>-0.322417</td>\n",
       "      <td>-0.384054</td>\n",
       "      <td>1.133769</td>\n",
       "      <td>-1.099891</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-02  0.865408 -2.301539  1.744812 -0.761207\n",
       "2022-01-03  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-01-04 -0.322417 -0.384054  1.133769 -1.099891\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "thres = .4\n",
    "df.query('A>@thres or D<-1*@thres')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- .isin相当于sql中的in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04</th>\n",
       "      <td>-0.322417</td>\n",
       "      <td>-0.384054</td>\n",
       "      <td>1.133769</td>\n",
       "      <td>-1.099891</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-04 -0.322417 -0.384054  1.133769 -1.099891"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.index.isin(['2022-02-01','2022-01-01','2022-01-04'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True,  True, False,  True,  True,  True,  True,  True,\n",
       "        True])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "~df.index.isin(['2022-01-01','2022-01-04'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-02</th>\n",
       "      <td>0.865408</td>\n",
       "      <td>-2.301539</td>\n",
       "      <td>1.744812</td>\n",
       "      <td>-0.761207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-05</th>\n",
       "      <td>-0.172428</td>\n",
       "      <td>-0.877858</td>\n",
       "      <td>0.042214</td>\n",
       "      <td>0.582815</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-06</th>\n",
       "      <td>-1.100619</td>\n",
       "      <td>1.144724</td>\n",
       "      <td>0.901591</td>\n",
       "      <td>0.502494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-08</th>\n",
       "      <td>-0.267888</td>\n",
       "      <td>0.530355</td>\n",
       "      <td>-0.691661</td>\n",
       "      <td>-0.396754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-09</th>\n",
       "      <td>-0.687173</td>\n",
       "      <td>-0.845206</td>\n",
       "      <td>-0.671246</td>\n",
       "      <td>-0.012665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-10</th>\n",
       "      <td>-1.117310</td>\n",
       "      <td>0.234416</td>\n",
       "      <td>1.659802</td>\n",
       "      <td>0.742044</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-02  0.865408 -2.301539  1.744812 -0.761207\n",
       "2022-01-03  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-01-05 -0.172428 -0.877858  0.042214  0.582815\n",
       "2022-01-06 -1.100619  1.144724  0.901591  0.502494\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769\n",
       "2022-01-08 -0.267888  0.530355 -0.691661 -0.396754\n",
       "2022-01-09 -0.687173 -0.845206 -0.671246 -0.012665\n",
       "2022-01-10 -1.117310  0.234416  1.659802  0.742044"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[~df.index.isin(['2022-01-01','2022-01-04'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-02</th>\n",
       "      <td>0.865408</td>\n",
       "      <td>-2.301539</td>\n",
       "      <td>1.744812</td>\n",
       "      <td>-0.761207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04</th>\n",
       "      <td>-0.322417</td>\n",
       "      <td>-0.384054</td>\n",
       "      <td>1.133769</td>\n",
       "      <td>-1.099891</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-05</th>\n",
       "      <td>-0.172428</td>\n",
       "      <td>-0.877858</td>\n",
       "      <td>0.042214</td>\n",
       "      <td>0.582815</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-06</th>\n",
       "      <td>-1.100619</td>\n",
       "      <td>1.144724</td>\n",
       "      <td>0.901591</td>\n",
       "      <td>0.502494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-08</th>\n",
       "      <td>-0.267888</td>\n",
       "      <td>0.530355</td>\n",
       "      <td>-0.691661</td>\n",
       "      <td>-0.396754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-09</th>\n",
       "      <td>-0.687173</td>\n",
       "      <td>-0.845206</td>\n",
       "      <td>-0.671246</td>\n",
       "      <td>-0.012665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-10</th>\n",
       "      <td>-1.117310</td>\n",
       "      <td>0.234416</td>\n",
       "      <td>1.659802</td>\n",
       "      <td>0.742044</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-02  0.865408 -2.301539  1.744812 -0.761207\n",
       "2022-01-03  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-01-04 -0.322417 -0.384054  1.133769 -1.099891\n",
       "2022-01-05 -0.172428 -0.877858  0.042214  0.582815\n",
       "2022-01-06 -1.100619  1.144724  0.901591  0.502494\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769\n",
       "2022-01-08 -0.267888  0.530355 -0.691661 -0.396754\n",
       "2022-01-09 -0.687173 -0.845206 -0.671246 -0.012665\n",
       "2022-01-10 -1.117310  0.234416  1.659802  0.742044"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-08</th>\n",
       "      <td>-0.267888</td>\n",
       "      <td>0.530355</td>\n",
       "      <td>-0.691661</td>\n",
       "      <td>-0.396754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-09</th>\n",
       "      <td>-0.687173</td>\n",
       "      <td>-0.845206</td>\n",
       "      <td>-0.671246</td>\n",
       "      <td>-0.012665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-08 -0.267888  0.530355 -0.691661 -0.396754\n",
       "2022-01-09 -0.687173 -0.845206 -0.671246 -0.012665\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.nsmallest(4, 'C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>0.900856</td>\n",
       "      <td>-0.683728</td>\n",
       "      <td>-0.122890</td>\n",
       "      <td>-0.935769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-02</th>\n",
       "      <td>0.865408</td>\n",
       "      <td>-2.301539</td>\n",
       "      <td>1.744812</td>\n",
       "      <td>-0.761207</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-01-07  0.900856 -0.683728 -0.122890 -0.935769\n",
       "2022-01-02  0.865408 -2.301539  1.744812 -0.761207"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.nlargest(3, 'A')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 习题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-02-01</th>\n",
       "      <td>0.865408</td>\n",
       "      <td>-2.301539</td>\n",
       "      <td>1.744812</td>\n",
       "      <td>-0.761207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-03-01</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-04-01</th>\n",
       "      <td>-0.322417</td>\n",
       "      <td>-0.384054</td>\n",
       "      <td>1.133769</td>\n",
       "      <td>-1.099891</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-01</th>\n",
       "      <td>-0.172428</td>\n",
       "      <td>-0.877858</td>\n",
       "      <td>0.042214</td>\n",
       "      <td>0.582815</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-02-01  0.865408 -2.301539  1.744812 -0.761207\n",
       "2022-03-01  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-04-01 -0.322417 -0.384054  1.133769 -1.099891\n",
       "2022-05-01 -0.172428 -0.877858  0.042214  0.582815"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(1)\n",
    "df = pd.DataFrame(np.random.randn(5,4), \n",
    "                  columns=list('ABCD'),\n",
    "                  index = pd.date_range('20220101', freq = 'MS',periods=5))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-03-01</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-01</th>\n",
       "      <td>-0.172428</td>\n",
       "      <td>-0.877858</td>\n",
       "      <td>0.042214</td>\n",
       "      <td>0.582815</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-03-01  0.319039 -0.249370  1.462108 -2.060141\n",
       "2022-05-01 -0.172428 -0.877858  0.042214  0.582815"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.index.strftime('%b').map(lambda x:x.startswith('M'))]\n",
    "# df[df.index.strftime('%b').str.startswith(\"M\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-01</th>\n",
       "      <td>1.624345</td>\n",
       "      <td>-0.611756</td>\n",
       "      <td>-0.528172</td>\n",
       "      <td>-1.072969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-03-01</th>\n",
       "      <td>0.319039</td>\n",
       "      <td>-0.249370</td>\n",
       "      <td>1.462108</td>\n",
       "      <td>-2.060141</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2022-01-01  1.624345 -0.611756 -0.528172 -1.072969\n",
       "2022-03-01  0.319039 -0.249370  1.462108 -2.060141"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.query('A+B>0 and C-D>0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当i=-1.0时，满足条件的行数是3\n",
      "当i=-0.8时，满足条件的行数是3\n",
      "当i=-0.6时，满足条件的行数是2\n",
      "当i=-0.4时，满足条件的行数是2\n",
      "当i=-0.2时，满足条件的行数是2\n",
      "当i=0.0时，满足条件的行数是2\n",
      "当i=0.2时，满足条件的行数是1\n",
      "当i=0.4时，满足条件的行数是1\n",
      "当i=0.6时，满足条件的行数是0\n",
      "当i=0.8时，满足条件的行数是0\n",
      "当i=1.0时，满足条件的行数是0\n"
     ]
    }
   ],
   "source": [
    "for i in np.linspace(-1,1,11):\n",
    "    tmp = len(df.query('A+B>@i and C-D>@i'))\n",
    "    print(f'当i={round(i,2)}时，满足条件的行数是{tmp}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
